package com.vnp.cooking.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vnp.cooking.R;
import com.vnp.cooking.bean.ObjCookpad;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class HoziListAdapter extends BaseAdapter{
	
	private Context context;
	private LayoutInflater inflater;
	private ArrayList<ObjCookpad> objCookpads;
	private DisplayImageOptions options = null;
	private ImageLoader imageLoader;
	
	
	public HoziListAdapter(Context context, ArrayList<ObjCookpad> objCookpads, ImageLoader imageLoader) {
		super();
		this.context = context;
		this.objCookpads = objCookpads;
		this.imageLoader = imageLoader;
		this.inflater = LayoutInflater.from(context);
		if (this.options == null)
			this.options = new DisplayImageOptions.Builder()
			.cacheOnDisc(true)
			.cacheInMemory(true)
			.showImageForEmptyUri(R.drawable.image_not_avaiable)
			.showImageOnFail(R.drawable.image_not_avaiable)
			.showImageOnLoading(R.drawable.image_not_avaiable)
			.resetViewBeforeLoading(true)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build(); 
		
	}
	
	@Override
	public int getCount() {
		return objCookpads.size();
	}

	@Override
	public Object getItem(int position) {
		return objCookpads.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			
			v = inflater.inflate(R.layout.hz_item, parent, false);
			
			holder.imgMonan = (ImageView) v.findViewById(R.id.img_item_hz);
			holder.position = position;
			
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		imageLoader.displayImage(objCookpads.get(position).getUrlImageCookpad(), holder.imgMonan, options);
		return v;
	}
	
	// optimize listview
	class ViewHolder {
		ImageView imgMonan;
		int position;
	}

}
